package com.zhiyou100.action;

import com.zhiyou100.entity.Student;
import com.zhiyou100.service.StudentService;
import com.zhiyou100.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * @author yang
 * @className StudentAction
 * @packageName com.zhiyou100.action
 * @Description TODO 学生 action
 * @date 2020-08-26 14:18
 */
@Controller
@RequestMapping("/student")
public class StudentAction {
    private StudentService studentService;

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @RequestMapping("/getAll.action")
    public String getAll(Model model) {
        final List<Student> students = studentService.listStudents();
        model.addAttribute("studentList", students);
        return "index";
    }


    @RequestMapping("/upload.action")
    public String upload(@RequestParam("names") CommonsMultipartFile names) throws IOException {
        final InputStream in = names.getInputStream();
        final BufferedReader bin = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));

        while (true) {
            final String line = bin.readLine();
            if (line == null) {
                break;
            }

            if (line.length() == 0) {
                continue;
            }

            // 使用 空格，截取字符串
            final String[] split = line.split(" +");

            System.out.println(Arrays.toString(split));

            final Student student = new Student();
            student.setSname(split[0]);
            student.setSx(Integer.valueOf(split[1]));
            student.setSy(Integer.valueOf(split[2]));
            studentService.addOne(student);
        }
        return "redirect:/student/getAll.action";
    }

    @RequestMapping("/login.action")
    public String loginMethod(String sname, String spwd, HttpServletRequest request) {
        // 根据账号查询
        final Student stu = studentService.getOneBySname(sname);

        final String key = request.getServletContext().getInitParameter("key");

        if (stu.getSpwd() == null || stu.getSpwd().trim().length() == 0) {
            // 为空，说明是第一次登陆，把密码加入此用户的列表中
            final String md5Spwd = Md5Util.md5(spwd, key);
            stu.setSpwd(md5Spwd);
            studentService.changeSpwd(stu);
        }

        // 判断登陆是否成功
        if (Md5Util.verify(spwd, key, stu.getSpwd())) {
            request.getSession().setAttribute("stu", stu);
            return "show_exam";
        }
        // 登陆不成功
        request.setAttribute("message", "账号或者密码错误！！");
        return "student_login";
    }


}
